package com.onlinelearn.chatservice.controller;

import com.onlinelearn.chatservice.dto.MessageDTO;
import com.onlinelearn.chatservice.model.ChatRoom;
import com.onlinelearn.chatservice.service.ChatRoomService;
import com.onlinelearn.chatservice.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/chat")
public class ChatController {
    @Autowired
    private MessageService messageService;

    @Autowired
    private ChatRoomService chatRoomService;

    @PostMapping("/messages")
    public ResponseEntity<MessageDTO> sendMessage(@RequestBody MessageDTO messageDTO) {
        MessageDTO sentMessage = messageService.sendMessage(messageDTO);
        return ResponseEntity.ok(sentMessage);
    }

    @GetMapping("/rooms/{chatRoomId}/unread")
    public ResponseEntity<List<MessageDTO>> getUnreadMessages(@PathVariable Long chatRoomId) {
        List<MessageDTO> unreadMessages = messageService.getUnreadMessages(chatRoomId);
        return ResponseEntity.ok(unreadMessages);
    }

    @PostMapping("/rooms")
    public ResponseEntity<ChatRoom> createChatRoom(
            @RequestParam Long courseId,
            @RequestParam Long teacherId
    ) {
        ChatRoom chatRoom = chatRoomService.createChatRoom(courseId, teacherId);
        return ResponseEntity.ok(chatRoom);
    }

    @PostMapping("/rooms/{chatRoomId}/participants")
    public ResponseEntity<Void> addParticipant(
            @PathVariable Long chatRoomId,
            @RequestParam Long userId
    ) {
        chatRoomService.addParticipant(chatRoomId, userId);
        return ResponseEntity.ok().build();
    }
} 